import axios from 'axios'
import { Notification } from 'element-ui'
import { toLogin } from './commonFunction'
const service = axios.create({
  baseURL: process.env.NODE_ENV !== "development" ? '/gaixc/duanmianMonitor' : '',
  timeout: 30000,
  // baseURL: process.env.baseURL
  headers: {
    'Content-Type': 'application/json',
    'token': localStorage.getItem('accessToken')
  }
})

service.interceptors.request.use(config => {
  // 添加token
  config.headers['token'] = localStorage.getItem('accessToken');
  return config
}, error => {
  Notification.error({
    title: '错误',
    message: error
  })
  return Promise.reject(error)
})

service.interceptors.response.use(response => {
  console.log('response', response)
  if (response && response.headers) {
    const newToken = response.headers["New-Access-Token"]
    // console.log('newToken1111', response);
    if (newToken) {
      localStorage.setItem('accessToken', newToken)
    }
  }
  if (response.data.code == 200) {
    return response.data
  } else if (response.data.code == 99 || response.data.code == 401) { // 未登录
    Notification.error(response.data.errorMsg || response.data.msg || response.data.message)
    toLogin();
    return response.data
  } else {
    console.log('response', response.data instanceof Blob)
    if (response.data instanceof Blob) {
      return response.data
    } else {
      console.log('response', response)
      Notification.error(response.data.errorMsg || response.data.msg || response.data.message)
      console.log('responsesss', response)
      return response.data
    }
  }
}, error => {
  console.log('error', error)
  const errorInfo = error.response

  if (error.code === 'ECONNABORTED') {
    // 请求超时
    Notification.error({
      title: '错误',
      message: '请求超时，请联系管理员！'
    })
  }

  let errorText = ''
  if (errorInfo) {
    switch (errorInfo.status) {
      case 401:
        errorText = 'token失效，请重新登录';
        break
      case 403:
        errorText = '网络不稳定,请联系管理员'
        break
      case 500:
        errorText = '网络不稳定,请联系管理员'
        break
      case 504:
        errorText = '网络请求超时'
        break;
      case 501:
        errorText = 'token失效，请重新登录';
        break
    }
    // Notification.error({
    //   title: '错误',
    //   message: errorText
    // })
    console.error(errorText)
  }
  return false
  // return Promise.reject(error)

})


export const svgRequest = axios.create({
  baseURL: process.env.NODE_ENV !== "development" ? '/' : '',
  timeout: 30000,
  // baseURL: process.env.baseURL
  headers: {
    'Content-Type': 'application/json',
    'token': localStorage.getItem('accessToken')
  }
})

svgRequest.interceptors.request.use(config => {
  // 添加token
  config.headers['token'] = localStorage.getItem('accessToken');
  return config
}, error => {
  Notification.error({
    title: '错误',
    message: error
  })
  return Promise.reject(error)
})

svgRequest.interceptors.response.use(response => {
  const res = response.data
  if (res && res.headers) {
    const newToken = res.headers["New-Access-Token"]
    console.log(newToken);
    if (newToken) {
      localStorage.setItem('accessToken', newToken)
    }
  }

  return res
}, error => {
  const errorInfo = error.response

  if (error.code === 'ECONNABORTED') {
    // 请求超时
    Notification.error({
      title: '错误',
      message: '请求超时，请联系管理员！'
    })
  }

  let errorText = ''
  if (errorInfo) {
    switch (errorInfo.status) {
      case 401:
        // 跳转登录页
        break
      case 403:
        errorText = '网络不稳定,请联系管理员'
        break
      case 500:
        errorText = '网络不稳定,请联系管理员'
        break
      case 504:
        errorText = '网络请求超时'
        break
    }
    // Notification.error({
    //   title: '错误',
    //   message: errorText
    // })
    console.error(errorText)
  }
  return false
  // return Promise.reject(error)

})


export default service